package com.feign.utils;



import org.springframework.stereotype.Component;


import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Component
public class GetUtils {

    /**
     * 获取请求体中的参数封装成map
     *
     * @param request 请求
     * @return map
     */
    public static Map<String, Object> getParamsWithMapByRequest(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>(30);
        // 所有请求参数集合map
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 遍历map中的每一键值对
        for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
            // 判断值是否为空
            if (!Arrays.asList(stringEntry.getValue()).get(0).isEmpty()) {
                // 获取键
                String key = stringEntry.getKey();
                // 获取值
                String value = request.getParameter(stringEntry.getKey());
                params.put(key, value);
            }
        }
        return params;
    }

}
